<!DOCTYPE html><html lang="zh-Hans" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>水花和涟漪 | MonsterTang</title><meta name="keywords" content="Shader,Environment"><meta name="author" content="MonsterTang"><meta name="copyright" content="MonsterTang"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><meta name="description" content="效果：在场景中物体表面溅起水花，水面上荡起涟漪。 生成深度图场景顶部一个摄像机（RainCamera）俯拍整个场景，将场景的深度信息存储在一张图中。 深度图shader123456789101112131415161718192021222324252627282930313233Shader &quot;Custom&#x2F;DepthByReplaceShader&quot;&amp;#123;    Sub">
<meta property="og:type" content="article">
<meta property="og:title" content="水花和涟漪">
<meta property="og:url" content="http://www.monstertang.club/2017/04/15/%E6%B0%B4%E8%8A%B1%E5%92%8C%E6%B6%9F%E6%BC%AA/index.html">
<meta property="og:site_name" content="MonsterTang">
<meta property="og:description" content="效果：在场景中物体表面溅起水花，水面上荡起涟漪。 生成深度图场景顶部一个摄像机（RainCamera）俯拍整个场景，将场景的深度信息存储在一张图中。 深度图shader123456789101112131415161718192021222324252627282930313233Shader &quot;Custom&#x2F;DepthByReplaceShader&quot;&amp;#123;    Sub">
<meta property="og:locale">
<meta property="og:image" content="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1583142178222&di=842ca735d1814a823176896bdaf42169&imgtype=0&src=http%3A%2F%2Fpic3.16pic.com%2F00%2F15%2F83%2F16pic_1583861_b.jpg">
<meta property="article:published_time" content="2017-04-15T02:36:20.000Z">
<meta property="article:modified_time" content="2020-11-20T09:52:40.105Z">
<meta property="article:author" content="MonsterTang">
<meta property="article:tag" content="Shader">
<meta property="article:tag" content="Environment">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1583142178222&di=842ca735d1814a823176896bdaf42169&imgtype=0&src=http%3A%2F%2Fpic3.16pic.com%2F00%2F15%2F83%2F16pic_1583861_b.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://www.monstertang.club/2017/04/15/%E6%B0%B4%E8%8A%B1%E5%92%8C%E6%B6%9F%E6%BC%AA/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true},
  copy: {
    success: 'Copy successfully',
    error: 'Copy error',
    noSupport: 'The browser does not support'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: 'Just',
    min: 'minutes ago',
    hour: 'hours ago',
    day: 'days ago',
    month: 'months ago'
  },
  copyright: undefined,
  ClickShowText: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  justifiedGallery: {
    js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
    css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
};

var saveToLocal = {
  set: function setWithExpiry(key, value, ttl) {
    const now = new Date()
    const expiryDay = ttl * 86400000
    const item = {
      value: value,
      expiry: now.getTime() + expiryDay,
    }
    localStorage.setItem(key, JSON.stringify(item))
  },

  get: function getWithExpiry(key) {
    const itemStr = localStorage.getItem(key)

    if (!itemStr) {
      return undefined
    }
    const item = JSON.parse(itemStr)
    const now = new Date()

    if (now.getTime() > item.expiry) {
      localStorage.removeItem(key)
      return undefined
    }
    return item.value
  }
}</script><script id="config_change">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-11-20 17:52:40'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(function () {  window.activateDarkMode = function () {
    document.documentElement.setAttribute('data-theme', 'dark')
    if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
    }
  }
  window.activateLightMode = function () {
    document.documentElement.setAttribute('data-theme', 'light')
   if (document.querySelector('meta[name="theme-color"]') !== null) {
      document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
    }
  }
  const autoChangeMode = 'false'
  const t = saveToLocal.get('theme')
  if (autoChangeMode === '1') {
    const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
    const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
    const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
    const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified
    if (t === undefined) {
      if (isLightMode) activateLightMode()
      else if (isDarkMode) activateDarkMode()
      else if (isNotSpecified || hasNoSupport) {
        const now = new Date()
        const hour = now.getHours()
        const isNight = hour <= 6 || hour >= 18
        isNight ? activateDarkMode() : activateLightMode()
      }
      window.matchMedia('(prefers-color-scheme: dark)').addListener(function (e) {
        if (saveToLocal.get('theme') === undefined) {
          e.matches ? activateDarkMode() : activateLightMode()
        }
      })
    } else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else if (autoChangeMode === '2') {
    const now = new Date()
    const hour = now.getHours()
    const isNight = hour <= 6 || hour >= 18
    if (t === undefined) isNight ? activateDarkMode() : activateLightMode()
    else if (t === 'light') activateLightMode()
    else activateDarkMode()
  } else {
    if (t === 'dark') activateDarkMode()
    else if (t === 'light') activateLightMode()
  }const asideStatus = saveToLocal.get('aside-status')
if (asideStatus !== undefined) {
   if (asideStatus === 'hide') {
     document.documentElement.classList.add('hide-aside')
   } else {
     document.documentElement.classList.remove('hide-aside')
   }
}})()</script><meta name="generator" content="Hexo 5.2.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/null" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">9</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">8</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">Categories</div><div class="length-num">4</div></a></div></div></div><hr/></div></div><div id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1583142178222&amp;di=842ca735d1814a823176896bdaf42169&amp;imgtype=0&amp;src=http%3A%2F%2Fpic3.16pic.com%2F00%2F15%2F83%2F16pic_1583861_b.jpg)"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">MonsterTang</a></span><span id="menus"><span class="close" id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></span></span></nav><div id="post-info"><h1 class="post-title">水花和涟漪</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="fa-fw post-meta-icon far fa-calendar-alt"></i><span class="post-meta-label">Created</span><time datetime="2017-04-15T02:36:20.000Z" title="undefined 2017-04-15 10:36:20">2017-04-15</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91/">游戏开发</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91/Unity/">Unity</a></span></div><div class="meta-secondline"> <span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">Word count:</span><span class="word-count">924</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">Reading time:</span><span>4min</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">Post View:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><p>效果：<br>在场景中物体表面溅起水花，水面上荡起涟漪。</p>
<h4 id="生成深度图"><a href="#生成深度图" class="headerlink" title="生成深度图"></a>生成深度图</h4><p>场景顶部一个摄像机（RainCamera）俯拍整个场景，将场景的深度信息存储在一张图中。</p>
<h5 id="深度图shader"><a href="#深度图shader" class="headerlink" title="深度图shader"></a>深度图shader</h5><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">Shader <span class="string">&quot;Custom/DepthByReplaceShader&quot;</span></span><br><span class="line">&#123;</span><br><span class="line">    SubShader</span><br><span class="line">    &#123;</span><br><span class="line">        Tags&#123; <span class="string">&quot;RenderType&quot;</span> = <span class="string">&quot;Opaque&quot;</span> &#125;</span><br><span class="line">        Pass&#123;</span><br><span class="line">        Fog&#123; Mode Off &#125;</span><br><span class="line">        CGPROGRAM</span><br><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> vertex vert</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">pragma</span> fragment frag</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;UnityCG.cginc&quot;</span></span></span><br><span class="line">    struct v2f &#123;</span><br><span class="line">        float4 pos : SV_POSITION;</span><br><span class="line">        float2 depth : TEXCOORD0;</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="function">v2f <span class="title">vert</span><span class="params">(appdata_base v)</span> </span>&#123;</span><br><span class="line">        v2f o;</span><br><span class="line">        <span class="comment">//o.pos = UnityObjectToClipPos(v.vertex);</span></span><br><span class="line">        o.pos = mul(UNITY_MATRIX_MVP, v.vertex);</span><br><span class="line">        <span class="comment">//UNITY_TRANSFER_DEPTH(o.depth);</span></span><br><span class="line">        o.depth = o.pos.zw;</span><br><span class="line">        <span class="keyword">return</span> o;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function">float4 <span class="title">frag</span><span class="params">(v2f i)</span> : COLOR</span>&#123;</span><br><span class="line">        <span class="comment">//return EncodeFloatRGBA(i.depth);</span></span><br><span class="line">        <span class="comment">//UNITY_OUTPUT_DEPTH(i.depth);</span></span><br><span class="line">        <span class="keyword">float</span> d = (i.depth.x / i.depth.y);</span><br><span class="line">        <span class="keyword">return</span> float4(d,d,d,<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">        ENDCG</span><br><span class="line">    &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure>
<h5 id="按layer生成深度图"><a href="#按layer生成深度图" class="headerlink" title="按layer生成深度图"></a>按layer生成深度图</h5><p>改变layer，分别生成不同layer下的深度图。<br><figure class="highlight cs"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">using</span> UnityEngine;</span><br><span class="line"><span class="keyword">using</span> System.Collections;</span><br><span class="line"><span class="keyword">using</span> System.IO;</span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title">DepthRender</span> : <span class="title">MonoBehaviour</span></span><br><span class="line">&#123;</span><br><span class="line">    <span class="keyword">private</span> Camera DepthCamera;</span><br><span class="line">    <span class="keyword">private</span> Shader depthShader;</span><br><span class="line">    <span class="keyword">private</span> Texture2D tex;</span><br><span class="line">    [<span class="meta">ContextMenu(<span class="meta-string">&quot;DrawDepthMap&quot;</span>)</span>]</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">DrawDepthMap</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function"></span>    &#123;</span><br><span class="line">        <span class="comment">//先画NoWater，再画</span></span><br><span class="line">        DepthCamera = GetComponent&lt;Camera&gt;();</span><br><span class="line">        DepthCamera.depthTextureMode = DepthTextureMode.DepthNormals;</span><br><span class="line">        DepthCamera.cullingMask = <span class="number">-1</span> &amp; (~(<span class="number">1</span> &lt;&lt; LayerMask.NameToLayer(<span class="string">&quot;Water&quot;</span>)));</span><br><span class="line">        DepthCamera.targetTexture = <span class="keyword">new</span> RenderTexture(<span class="number">1024</span>,<span class="number">1024</span>,<span class="number">24</span>);</span><br><span class="line">        depthShader = Shader.Find(<span class="string">&quot;Custom/DepthByReplaceShader&quot;</span>); ;</span><br><span class="line">        DepthCamera.SetReplacementShader(depthShader, <span class="string">&quot;&quot;</span>);</span><br><span class="line">        RenderTexture.active = DepthCamera.targetTexture;</span><br><span class="line">        RenderAndWrite(<span class="string">&quot;depthnowater&quot;</span>);</span><br><span class="line">        DepthCamera.cullingMask = <span class="number">1</span> &lt;&lt; LayerMask.NameToLayer(<span class="string">&quot;Water&quot;</span>);</span><br><span class="line">        RenderAndWrite(<span class="string">&quot;depthwater&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">RenderAndWrite</span>(<span class="params"><span class="built_in">string</span> fileName</span>)</span></span><br><span class="line"><span class="function"></span>    &#123;</span><br><span class="line">        DepthCamera.Render();</span><br><span class="line">        tex = <span class="keyword">new</span> Texture2D(<span class="number">1024</span>, <span class="number">1024</span>);</span><br><span class="line">        tex.ReadPixels(<span class="keyword">new</span> Rect(<span class="number">0</span>, <span class="number">0</span>, <span class="number">1024</span>, <span class="number">1024</span>), <span class="number">0</span>, <span class="number">0</span>);</span><br><span class="line">        tex.Apply();</span><br><span class="line">        <span class="built_in">byte</span>[] bytes = tex.EncodeToPNG();</span><br><span class="line">        FileStream fsStream = <span class="keyword">new</span> FileStream(Application.dataPath + <span class="string">&quot;/Resources/skybox/ArtSource/Environment&quot;</span> + <span class="string">&quot;/&quot;</span> + fileName + <span class="string">&quot;.png&quot;</span>, FileMode.Create);</span><br><span class="line">        fsStream.Write(bytes, <span class="number">0</span>, bytes.Length);</span><br><span class="line">        fsStream.Close();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;  </span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>SetReplacementShader是给摄像机指定shader，其面向的顶点是所有能拍到的顶点。<br>拍摄到一张RenderTexture的做法。<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">DepthCamera.targetTexture = <span class="keyword">new</span> RenderTexture(<span class="number">1024</span>,<span class="number">1024</span>,<span class="number">24</span>);</span><br><span class="line">DepthCamera.Render();</span><br></pre></td></tr></table></figure><br>RenderTexture太庞大，可以转成png存储。<code>RenderTexture.active</code>设置之后，tex.ReadPixels可以从当前active的RenderTexture中读取像素信息。</p>
</blockquote>
<h4 id="水花生成规则"><a href="#水花生成规则" class="headerlink" title="水花生成规则"></a>水花生成规则</h4><p>以主摄像机（MainCamera）为圆心，半径r的圆为水花生成范围。<br>随机生成一个水花位置：<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">Vector3 camWorldPos = Camera.main.transform.position;</span><br><span class="line">centerPoints[i].x = Random.Range(-radius, radius);</span><br><span class="line">centerPoints[i].z = Random.Range(-radius, radius);</span><br><span class="line">centerPoints[i].y = BillboardHalfSize;</span><br></pre></td></tr></table></figure><br>水花贴图四个顶点的高度可能不一样，这样判断深度是否满足要求可能会有歧义，故将水花中心点作为深度判断依据，而把外围展开的信息放在normal中以在shader里处理。<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Vector3 right = Vector3.Cross(look, up);</span><br><span class="line">        normals[i * <span class="number">4</span>] = right * BillboardHalfSize - up * qSSplashParam.BillboardHalfSize;</span><br><span class="line">        normals[i * <span class="number">4</span> + <span class="number">1</span>] = -right * BillboardHalfSize - up * qSSplashParam.BillboardHalfSize;</span><br><span class="line">        normals[i * <span class="number">4</span> + <span class="number">2</span>] = -right * BillboardHalfSize + up * qSSplashParam.BillboardHalfSize;</span><br><span class="line">        normals[i * <span class="number">4</span> + <span class="number">3</span>] = right * BillboardHalfSize + up * qSSplashParam.BillboardHalfSize;</span><br></pre></td></tr></table></figure><br>涟漪和水花大致相似，区别是水花在竖直面上展开，而涟漪是在水平面展开，另外水花需要向上提一个半径的高度。<br>这里希望水花之间的动画彼此独立，使用一个QSWaveMgr来记录水花的时间信息，并使用uv2来传递这些数据。（将passedTime/totalTime传给shader计算当前帧动画位于第几帧）</p>
<h4 id="深度校准"><a href="#深度校准" class="headerlink" title="深度校准"></a>深度校准</h4><p>上一步生成的位置是以摄像机为圆心的xoy平面，深度信息是和场景不一致的。我们只需要通过该位置的xy值，去刚才生成的深度图中去查找正确的深度值进行校准，即可得到场景中水花的正确位置。<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="function">float4 <span class="title">AdjustPosByCollision</span><span class="params">(float4 worldPos)</span></span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        float4 shadowPos = mul(_LightViewProj, worldPos);</span><br><span class="line">        shadowPos.xy /= shadowPos.w;</span><br><span class="line">        float2 posNDC = clamp(shadowPos.xy, <span class="number">-1.0</span>, <span class="number">1.0</span>);</span><br><span class="line">        <span class="keyword">if</span> (posNDC.x - shadowPos.x + posNDC.y - shadowPos.y != <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">return</span> float4(<span class="number">-100000</span>, <span class="number">-100000</span>, <span class="number">-100000</span>, <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        shadowPos.x = shadowPos.x * <span class="number">0.5f</span> + <span class="number">0.5f</span>;</span><br><span class="line">        shadowPos.y = shadowPos.y * <span class="number">0.5f</span> + <span class="number">0.5f</span>;</span><br><span class="line">        <span class="keyword">float</span> collisionDepth = UNITY_SAMPLE_DEPTH(tex2Dlod(_CollisionDepthMap, float4(shadowPos.x, shadowPos.y, <span class="number">0</span>, <span class="number">0</span>)));</span><br><span class="line">        collisionDepth = (collisionDepth - <span class="number">0.5f</span>) / <span class="number">0.5f</span>;</span><br><span class="line">        <span class="comment">//collisionDepth = 0.01f;</span></span><br><span class="line">        float4 newWorldPos = mul(_LightViewProjInv, float4(posNDC.x, posNDC.y, collisionDepth, <span class="number">1.0</span>));</span><br><span class="line">        newWorldPos = newWorldPos / newWorldPos.w;</span><br><span class="line">        <span class="keyword">return</span> newWorldPos;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure></p>
<blockquote>
<p>_LightViewProj是RainCamera.projectionMatrix * RainCamera.worldToCameraMatrix<br>也就是将世界坐标转换成摄像机的裁剪坐标，然后将x,y[-1,1]映射到[0,1],以(x,y)为uv值取出深度进行校准。</p>
</blockquote>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">Author: </span><span class="post-copyright-info"><a href="mailto:undefined">MonsterTang</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">Link: </span><span class="post-copyright-info"><a href="http://www.monstertang.club/2017/04/15/%E6%B0%B4%E8%8A%B1%E5%92%8C%E6%B6%9F%E6%BC%AA/">http://www.monstertang.club/2017/04/15/%E6%B0%B4%E8%8A%B1%E5%92%8C%E6%B6%9F%E6%BC%AA/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">Copyright Notice: </span><span class="post-copyright-info">All articles in this blog are licensed under <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> unless stating additionally.</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Shader/">Shader</a><a class="post-meta__tags" href="/tags/Environment/">Environment</a></div><div class="post_share"><div class="social-share" data-image="https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1583142178222&amp;di=842ca735d1814a823176896bdaf42169&amp;imgtype=0&amp;src=http%3A%2F%2Fpic3.16pic.com%2F00%2F15%2F83%2F16pic_1583861_b.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2017/05/11/%E5%9C%BA%E6%99%AF%E6%97%A0%E7%BC%9D%E5%88%87%E6%8D%A2/"><img class="prev-cover" src="https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1583142515788&amp;di=cded44bfc2f8d9df9963924d2eba7f81&amp;imgtype=0&amp;src=http%3A%2F%2Farticle-fd.zol-img.com.cn%2Ft_s998x562c5%2Fg5%2FM00%2F09%2F0F%2FChMkJ1tpSNCIGfJPAAOwBG0ZbLEAAqiJAModZoAA7Ac695.jpg" onerror="onerror=null;src='/img/404.jpg'"><div class="pagination-info"><div class="label">Previous Post</div><div class="prev_info">场景无缝切换</div></div></a></div><div class="next-post pull-right"><a href="/2017/04/15/%E4%B8%8B%E9%9B%A8%E6%95%88%E6%9E%9C/"><img class="next-cover" src="https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1583142178222&amp;di=842ca735d1814a823176896bdaf42169&amp;imgtype=0&amp;src=http%3A%2F%2Fpic3.16pic.com%2F00%2F15%2F83%2F16pic_1583861_b.jpg" onerror="onerror=null;src='/img/404.jpg'"><div class="pagination-info"><div class="label">Next Post</div><div class="next_info">下雨效果</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> Related Articles</span></div><div class="relatedPosts-list"><div><a href="/2017/04/15/下雨效果/" title="下雨效果"><img class="cover" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1583142178222&di=842ca735d1814a823176896bdaf42169&imgtype=0&src=http%3A%2F%2Fpic3.16pic.com%2F00%2F15%2F83%2F16pic_1583861_b.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2017-04-15</div><div class="title">下雨效果</div></div></a></div><div><a href="/2017/04/04/雾/" title="雾"><img class="cover" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1583142306196&di=804a2d89838e2ff892d3da616cf6e937&imgtype=0&src=http%3A%2F%2Fimg1.qunarzz.com%2Ftravel%2Fd8%2F1601%2F6a%2Fe0f41a28511761f7.png_r_650x395x95_0a86ed0d.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2017-04-04</div><div class="title">雾</div></div></a></div></div></div></div><div class="aside_content" id="aside_content"><div class="card-widget card-info"><div class="card-content"><div class="card-info-avatar is-center"><img class="avatar-img" src="/null" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">MonsterTang</div><div class="author-info__description">MonsterTang</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">Articles</div><div class="length-num">9</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">Tags</div><div class="length-num">8</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">Categories</div><div class="length-num">4</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a></div></div><div class="card-widget card-announcement"><div class="card-content"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>Announcement</span></div><div class="announcement_content">This is my Blog</div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="card-content"><div class="item-headline"><i class="fas fa-stream"></i><span>Catalog</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E6%B7%B1%E5%BA%A6%E5%9B%BE"><span class="toc-number">1.</span> <span class="toc-text">生成深度图</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B7%B1%E5%BA%A6%E5%9B%BEshader"><span class="toc-number">1.1.</span> <span class="toc-text">深度图shader</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%8C%89layer%E7%94%9F%E6%88%90%E6%B7%B1%E5%BA%A6%E5%9B%BE"><span class="toc-number">1.2.</span> <span class="toc-text">按layer生成深度图</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B0%B4%E8%8A%B1%E7%94%9F%E6%88%90%E8%A7%84%E5%88%99"><span class="toc-number">2.</span> <span class="toc-text">水花生成规则</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B7%B1%E5%BA%A6%E6%A0%A1%E5%87%86"><span class="toc-number">3.</span> <span class="toc-text">深度校准</span></a></li></ol></div></div></div><div class="card-widget card-recent-post"><div class="card-content"><div class="item-headline"><i class="fas fa-history"></i><span>Recent Post</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2020/11/19/UE4%E8%B5%84%E6%BA%90%E5%8A%A0%E8%BD%BD%E5%8F%8A%E7%83%AD%E6%9B%B4/" title="UE4资源加载及热更"><img src="" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="UE4资源加载及热更"/></a><div class="content"><a class="title" href="/2020/11/19/UE4%E8%B5%84%E6%BA%90%E5%8A%A0%E8%BD%BD%E5%8F%8A%E7%83%AD%E6%9B%B4/" title="UE4资源加载及热更">UE4资源加载及热更</a><time datetime="2020-11-18T16:00:00.000Z" title="Created 2020-11-19 00:00:00">2020-11-19</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2020/03/23/AutomationSystem%E8%AF%A6%E8%A7%A3/" title="AutomationSystem详解以及UE4全自动测试工作流"><img src="https://docs.unrealengine.com/Images/Programming/Automation/UserGuide/AutomationToolbar.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="AutomationSystem详解以及UE4全自动测试工作流"/></a><div class="content"><a class="title" href="/2020/03/23/AutomationSystem%E8%AF%A6%E8%A7%A3/" title="AutomationSystem详解以及UE4全自动测试工作流">AutomationSystem详解以及UE4全自动测试工作流</a><time datetime="2020-03-23T08:35:55.000Z" title="Created 2020-03-23 16:35:55">2020-03-23</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2019/01/06/Lua%E4%BC%98%E5%8C%96/" title="Lua优化"><img src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=259980519,2051658581&amp;fm=26&amp;gp=0.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Lua优化"/></a><div class="content"><a class="title" href="/2019/01/06/Lua%E4%BC%98%E5%8C%96/" title="Lua优化">Lua优化</a><time datetime="2019-01-06T06:04:03.000Z" title="Created 2019-01-06 14:04:03">2019-01-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2019/01/01/%E6%9C%80%E5%90%8E%E7%9A%84%E5%80%94%E5%BC%BA/" title="最后的倔强"><img src="https://raw.githubusercontent.com/tangcat123/picformonster/master/20200302151515.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="最后的倔强"/></a><div class="content"><a class="title" href="/2019/01/01/%E6%9C%80%E5%90%8E%E7%9A%84%E5%80%94%E5%BC%BA/" title="最后的倔强">最后的倔强</a><time datetime="2019-01-01T07:05:23.000Z" title="Created 2019-01-01 15:05:23">2019-01-01</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2018/05/21/%E4%BA%BA%E7%B1%BB%E7%AE%80%E5%8F%B2/" title="人类简史"><img src="https://timgsa.baidu.com/timg?image&amp;quality=80&amp;size=b9999_10000&amp;sec=1583142385859&amp;di=3e2fd0e2ac8c95a45c3efbf57e037ba8&amp;imgtype=0&amp;src=http%3A%2F%2Fpic4.zhimg.com%2Fv2-3c3c6690278ce9bb162fb484c15c7cce_1200x500.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="人类简史"/></a><div class="content"><a class="title" href="/2018/05/21/%E4%BA%BA%E7%B1%BB%E7%AE%80%E5%8F%B2/" title="人类简史">人类简史</a><time datetime="2018-05-21T12:47:50.000Z" title="Created 2018-05-21 20:47:50">2018-05-21</time></div></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 By MonsterTang</div><div class="framework-info"><span>Framework </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>Theme </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="Read Mode"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="Switch Between Light And Dark Mode"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="Setting"><i class="fas fa-cog"></i></button><button class="close" id="mobile-toc-button" type="button" title="Table Of Contents"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="Back To Top"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><div class="js-pjax"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script></div></body></html>